home *** CD-ROM | disk | FTP | other *** search
- /*
- Example program which uses some of the the Shell_* functions.
- It displays a times-table in a window, along with various
- labels and an animated bar-graph.
- */
-
-
-
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <stdio.h>
- #include <math.h>
-
-
- #include "Desklib:Error.h"
- #include "Desklib:WimpSWIs.h"
-
-
- #include "Shell.RectSave.h" /* For saving of data in rectangles */
- #include "Shell.TextRect.h" /* For displaying large amounts of text in a rect. in a window */
- #include "Shell.Printf.h" /* A substitute for printf. */
- #include "Shell.Array.h" /* For displaying 2D arrays. */
- #include "Shell.FontLabel.h" /* For displaying small 'labels' in an outline font. */
- #include "Shell.Label.h" /* For displaying small 'labels' in the system font. */
- #include "Shell.BlockRct.h" /* For displaying blocks which consist of small rectangles. */
- #include "Shell.Extra.h" /* For various low-level graphics utils. */
- #include "Shell.BarGraph.h" /* For a bar-graph display. */
- #include "Shell.PlainRect.h" /* For saving part of a Shell window as a sprite */
- #include "Shell.BarIcon.h" /* For an icon on the iconbar, and a default menu. */
- #include "Shell.SpriteRect.h" /* For displaying a sprite in the window. */
-
-
- #define PI 3.14159265
-
-
-
-
- static char *TimesTableRedrawer( int x, int y, Shell_GeneralArrayInfo *info)
- /* Returns a string which contains the contents of array location (x,y) */
- {
- static char s[64];
- sprintf( s, info->format, (x+1)*(y+1));
- return s;
- }
-
-
-
-
- static int BlockRedrawer( int x, int y, int maxx, int maxy, void *data)
- /* This helps plots a 2D gaussian by returning the colour of */
- /* the block at position (x,y). */
-
- {
- double dx = (double) x-maxx/2.0;
- double dy = (double) y-maxy/2.0;
-
- UNUSED( data);
-
- return (int) (8 - 7.99 * exp( -0.5 * (dx*dx+dy*dy)/50 ));
- }
-
-
-
-
- #define Rnd() ( (double) rand() / ( RAND_MAX+1.0))
- /* Returns random double y, 0 <= y <1 */
-
- #define RndInt( x) ( (int) (Rnd() * ((double) x)) )
- /* Returns random integer from { 0, 1, ... x-1 }. */
-
-
- /* The size of the sprite */
- #define SPRITE_X 512
- #define SPRITE_Y 512
-
-
-
-
-
- int main()
- {
-
- Shell_windblock *wind;
- Shell_rectblock *textrect, *barrect, *anglerect, *spriterect;
- int i;
- clock_t t = 0;
- int bars[100];
-
-
- Shell_Init( /*taskname */ "Shell Demomstration", /*resources*/ "ShellDemo");
- /* This calls Event_Initialise() etc. */
-
- Shell_AddBarIcon( "!shelldemo");
-
- Shell_InitRectSave( /* This registers a handler for clicks on any shell */
- "SaveAs",/*template name*/ /* window. If the click is Shift-Menu, and the pointer */
- 3, /* dragsprite */ /* is over a savable rectangle, a save box is opened. */
- 0, /* ok */ /* There is a template called 'SaveAs' in ShellEG's */
- 1, /* cancel */ /* 'Templates' file. */
- 2 /* filename */
- );
-
-
- /* TextRectPrintf-ing to a NULL windblock opens a default window */
- /* Shell_Printf is equivalent to Shell_TextRectPrintf( NULL, ...). */
-
- Shell_Printf( "This is the default text-output window.\n");
- Shell_Printf( "---------------------------------------\n\n");
- Shell_Printf( "Use Shift-Menu to save array/text rectangles...\n\n");
-
-
- wind = Shell_OpenGFXWindow();
- textrect = Shell_AddTextRect( wind, 30, -400, colour_BLACK, colour_YELLOW);
-
- Shell_TextRectPrintf( textrect, "Some text\n");
- Shell_SetWindowTitle( wind->window, "Shell Demonstration");
-
- Shell_FontLabel( wind, -100, 20, colour_BLACK, colour_GREY3, "Some anti-aliased text");
- Shell_FontLabel( wind, 800, Shell_TEXTYSIZE, colour_BLACK, colour_GREY1, "Times tables...");
-
- Shell_AddGeneralArray(
- wind,
- 800, 0, /* Position of top-left of the array */
- 12, 12, /* x and y size of array. */
- 9, /* Width of array's columns (in chrs). */
- "%i", /* This string is passed to the redrawer. */
- TimesTableRedrawer, /* Fn which returns text for any element of the array. */
- NULL, /* pointer that is passed to the redraw fn. */
- colour_BLACK, colour_ORANGE /* fore/back colour of the array. */
- );
-
- Shell_AddGeneralBlockRect( wind, 100, 100, 20, 20, 8, 8, BlockRedrawer, NULL);
- Shell_AddGeneralBlockRect( wind, 500, 100, 20, 20, 8, 8, BlockRedrawer, NULL);
- /* Displays an exponential-decay square, using 'BlockRedrawer' which returns */
- /* the brightness of any block in the rectangle. */
-
-
- barrect = Shell_AddBarGraph(
- wind,
- 30, -200, /* Position of bottom-left of bar graph */
- 30, /* Number of bars in the graph. */
- 16, 12, /* Spacing and width of bars. */
- 96, /* Maximum height of bars. */
- bars, /* Pointer to an array of 30 integers for bar heights */
- colour_RED, colour_GREY2
- );
- /* 'bars' is an array of the heights of each bar. Later, this array will be changed */
- /* and the bar-graph rectangle redrawn to give animation. */
-
-
-
- anglerect = Shell_Label( wind, 30, -50, colour_BLACK, colour_GREY3, "Angle = ");
- Shell_SetRectUpdateTime( anglerect, 0.5);
-
-
- /* Make a rect which can be saved as a sprite. */
- Shell_MakeRectIcon(
- Shell_AddPlainRect( wind, -150, -250, 550, 300),
- colour_TRANSPARENT, colour_TRANSPARENT,
- "r1" /* border */
- );
-
- Shell_Label( wind, -140, -300, colour_BLACK, colour_GREEN, "Try Shift-Menu inside the rectangle above here...");
-
-
-
- /* Make a sprite in the window. */
- spriterect = Shell_AddSpriteRect( wind, 800, 128, SPRITE_X, SPRITE_Y);
-
-
- /* The next bit animates a bar-graph... */
- /* and plot circles into the sprite. */
-
- for (;;) for ( i=0; i<100; i++) {
-
- int a;
-
- double angle = i/100.0 * 16 * PI;
-
- for ( a=0; a<30; a++) { /* Update the array of bar-heights */
- bars[a] = 48 + (int) ( 40 * ( sin( angle + a/2.0) * cos( angle/4 + a/3.0)));
- }
-
- Shell_ForceRectUpdate( barrect); /* Redraw the bargraph imediately without */
- /* clearing the rectangle. */
-
- Shell_ReLabelfSlow( anglerect, "Angle = %g", angle);
- /* Only updates at speed set earlier. */
-
- if ( clock()/CLOCKS_PER_SEC/10 != t/10) {
- t = clock()/CLOCKS_PER_SEC;
- Shell_Printf( "\nTime since start is now %i seconds\n\n", t);
- Shell_TextRectPrint( textrect, "Some more text...\n");
- }
-
- /* Plot a random circle into the spriterect... */
- {
- Shell_RedirectToSprite( spriterect);
- Wimp_SetColour( RndInt( 16));
- GFX_Move( RndInt( SPRITE_X), RndInt( SPRITE_Y));
- GFX_Plot(
- plot_CIRCLEFILL + ( RndInt(2) ? plot_DRAWRELINVERSE : plot_DRAWRELFORE),
- RndInt( SPRITE_X/4), 0
- );
- Shell_UnRedirect();
- Shell_ForceRectUpdate( spriterect);
- }
-
- Shell_PollSlow();
- }
-
- return 0;
- }
-